package edu.upc.fib.lamsandroid.client.fragment.lesson;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.text.Html;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.google.gson.Gson;
import edu.upc.fib.lamsandroid.client.R;
import edu.upc.fib.lamsandroid.client.Util;
import edu.upc.fib.lamsandroid.client.view.DraggableListView;
import edu.upc.fib.lamsandroid.server.api.model.ToolContent;
import edu.upc.fib.lamsandroid.server.api.model.ToolContentDataAssessment;
import edu.upc.fib.lamsandroid.server.api.model.ToolContentDataAssessmentQuestion;
import edu.upc.fib.lamsandroid.server.api.model.ToolContentDataAssessmentQuestionOption;

import java.util.ArrayList;
import java.util.List;

public class AssessmentLessonFragment extends Fragment {

    private final ToolContent toolContent;
    private int questionIndex;
    private List<Fragment> questionFragments;

    public AssessmentLessonFragment(ToolContent toolContent) {
        this.toolContent = toolContent;
        questionIndex = 0;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ToolContentDataAssessment assContent
                = new Gson().fromJson(toolContent.getData().getJson(), ToolContentDataAssessment.class);

        View view = inflater.inflate(R.layout.assessment, null);
        LinearLayout layout = (LinearLayout) view.findViewById(R.id.titleAndDescription);

        Util.addWebView(getActivity(), layout, assContent.getTitle());
        Util.addWebView(getActivity(), layout, assContent.getInstructions());

        questionFragments = new ArrayList<Fragment>();

        for (ToolContentDataAssessmentQuestion question : assContent.getQuestions()) {
            switch (question.getType()) {
                case ToolContentDataAssessmentQuestion.TYPE_MULTIPLE_CHOICE:
                    questionFragments.add(new MultipleChoiceInnerFragment(question));
                    break;
                case ToolContentDataAssessmentQuestion.TYPE_MATCHING_PAIRS:
                    questionFragments.add(new MatchingPairsInnerFragment(question));
                    break;
                case ToolContentDataAssessmentQuestion.TYPE_SHORT_ANSWER:
                    questionFragments.add(new ShortAnswerInnerFragment(question));
                    break;
                case ToolContentDataAssessmentQuestion.TYPE_NUMERICAL:
                    questionFragments.add(new NumericalInnerFragment(question));
                    break;
                case ToolContentDataAssessmentQuestion.TYPE_TRUE_FALSE:
                    questionFragments.add(new TrueFalseInnerFragment(question));
                    break;
                case ToolContentDataAssessmentQuestion.TYPE_ESSAY:
                    questionFragments.add(new EssayInnerFragment(question));
                    break;
                case ToolContentDataAssessmentQuestion.TYPE_ORDERING:
                    questionFragments.add(new OrderingInnerFragment(question));
                    break;
            }
        }

        Button buttonPrevious = (Button) view.findViewById(R.id.buttonPrevious);
        buttonPrevious.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(--AssessmentLessonFragment.this.questionIndex < 0) {
                    AssessmentLessonFragment.this.questionIndex =
                            AssessmentLessonFragment.this.questionFragments.size() - 1;
                }

                FragmentTransaction ft = getFragmentManager().beginTransaction();
                ft.replace(R.id.the_inner_frag, questionFragments.get(AssessmentLessonFragment.this.questionIndex));
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                ft.addToBackStack(null);
                ft.commit();
            }
        });

        Button buttonNext = (Button) view.findViewById(R.id.buttonNext);
        buttonNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(++AssessmentLessonFragment.this.questionIndex >= AssessmentLessonFragment.this.questionFragments.size()) {
                    AssessmentLessonFragment.this.questionIndex = 0;
                }

                FragmentTransaction ft = getFragmentManager().beginTransaction();
                ft.replace(R.id.the_inner_frag, questionFragments.get(AssessmentLessonFragment.this.questionIndex));
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                ft.addToBackStack(null);
                ft.commit();
            }
        });

        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.replace(R.id.the_inner_frag, questionFragments.get(0));
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        ft.addToBackStack(null);
        ft.commit();

        return view;
    }

    private class MultipleChoiceInnerFragment extends Fragment {
        private final ToolContentDataAssessmentQuestion question;

        public MultipleChoiceInnerFragment(ToolContentDataAssessmentQuestion question) {
            this.question = question;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            Context c = getActivity().getApplicationContext();

            LinearLayout layout = new LinearLayout(c);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT, 0);

            layout.setLayoutParams(params);
            layout.setOrientation(LinearLayout.VERTICAL);

            // title
            Util.addWebView(getActivity(), layout, question.getTitle());

            // body
            Util.addWebView(getActivity(), layout, question.getBody());

            // options
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_single_choice);
            for (ToolContentDataAssessmentQuestionOption option : question.getOptions()) {
                adapter.add(option.getStringValue());
            }

            ListView lv = new ListView(getActivity());
            lv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            lv.setAdapter(adapter);
            lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            layout.addView(lv);

            ScrollView scrollView = new ScrollView(getActivity());
            scrollView.addView(layout);
            return scrollView;
        }
    }


    private class MatchingPairsInnerFragment extends Fragment {
        private final ToolContentDataAssessmentQuestion question;

        public MatchingPairsInnerFragment(ToolContentDataAssessmentQuestion question) {
            this.question = question;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            Context c = getActivity().getApplicationContext();

            LinearLayout layout = new LinearLayout(c);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT, 0);

            layout.setLayoutParams(params);
            layout.setOrientation(LinearLayout.VERTICAL);

            // title
            Util.addWebView(getActivity(), layout, question.getTitle());

            // body
            Util.addWebView(getActivity(), layout, question.getBody());

            // options
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_single_choice);
            ArrayList<TableRow> rows = new ArrayList<TableRow>(question.getOptions().size());
            for (ToolContentDataAssessmentQuestionOption option : question.getOptions()) {
                adapter.add(option.getStringValue());


                TextView tv = new TextView(layout.getContext());
                tv.setText(Html.fromHtml(option.getSpecificQuestion()));
                //Util.addWebView(getActivity(), layout, option.getSpecificQuestion());

                TableRow row = new TableRow(getActivity());
                row.addView(tv);

                rows.add(row);
            }

            TableLayout tableLayout = new TableLayout(getActivity());
            for (TableRow row : rows) {
                Spinner spinner = new Spinner(getActivity());
                spinner.setAdapter(adapter);
                row.addView(spinner);
                tableLayout.addView(row);
            }
            layout.addView(tableLayout);

            ScrollView scrollView = new ScrollView(getActivity());
            scrollView.addView(layout);
            return scrollView;
        }
    }

    private class ShortAnswerInnerFragment extends Fragment {
        private final ToolContentDataAssessmentQuestion question;

        public ShortAnswerInnerFragment(ToolContentDataAssessmentQuestion question) {
            this.question = question;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            Context c = getActivity().getApplicationContext();

            LinearLayout layout = new LinearLayout(c);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT, 0);

            layout.setLayoutParams(params);
            layout.setOrientation(LinearLayout.VERTICAL);

            // title
            Util.addWebView(getActivity(), layout, question.getTitle());

            // body
            Util.addWebView(getActivity(), layout, question.getBody());

            // free text field
            EditText editText = new EditText(getActivity());
            editText.setHint("Escribe la respuesta (texto corto)");
            layout.addView(editText);

            ScrollView scrollView = new ScrollView(getActivity());
            scrollView.addView(layout);
            return scrollView;
        }
    }

    private class NumericalInnerFragment extends Fragment {
        private final ToolContentDataAssessmentQuestion question;

        public NumericalInnerFragment(ToolContentDataAssessmentQuestion question) {
            this.question = question;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            Context c = getActivity().getApplicationContext();

            LinearLayout layout = new LinearLayout(c);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT, 0);

            layout.setLayoutParams(params);
            layout.setOrientation(LinearLayout.VERTICAL);

            // title
            Util.addWebView(getActivity(), layout, question.getTitle());

            // body
            Util.addWebView(getActivity(), layout, question.getBody());

            // numeric text field
            EditText editText = new EditText(getActivity());
            editText.setHint("Escribe la respuesta (numérica con o sin decimales)");
            editText.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
            layout.addView(editText);

            ScrollView scrollView = new ScrollView(getActivity());
            scrollView.addView(layout);
            return scrollView;
        }
    }

    private class TrueFalseInnerFragment extends Fragment {
        private final ToolContentDataAssessmentQuestion question;

        public TrueFalseInnerFragment(ToolContentDataAssessmentQuestion question) {
            this.question = question;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            Context c = getActivity().getApplicationContext();

            LinearLayout layout = new LinearLayout(c);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT, 0);

            layout.setLayoutParams(params);
            layout.setOrientation(LinearLayout.VERTICAL);

            // title
            Util.addWebView(getActivity(), layout, question.getTitle());

            // body
            Util.addWebView(getActivity(), layout, question.getBody());

            // true false spinner
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_single_choice);
            adapter.add("True");
            adapter.add("False");

            Spinner spinner = new Spinner(getActivity());
            spinner.setAdapter(adapter);
            layout.addView(spinner);

            ScrollView scrollView = new ScrollView(getActivity());
            scrollView.addView(layout);
            return scrollView;
        }
    }

    private class EssayInnerFragment extends Fragment {
        private final ToolContentDataAssessmentQuestion question;

        public EssayInnerFragment(ToolContentDataAssessmentQuestion question) {
            this.question = question;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            Context c = getActivity().getApplicationContext();

            LinearLayout layout = new LinearLayout(c);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT, 0);

            layout.setLayoutParams(params);
            layout.setOrientation(LinearLayout.VERTICAL);

            // title
            Util.addWebView(getActivity(), layout, question.getTitle());

            // body
            Util.addWebView(getActivity(), layout, question.getBody());

            // free text field multiline
            EditText editText = new EditText(getActivity());
            editText.setHint("Escribe el texto (libre)");
            editText.setLines(10);
            layout.addView(editText);

            ScrollView scrollView = new ScrollView(getActivity());
            scrollView.addView(layout);
            return scrollView;
        }
    }

    private class OrderingInnerFragment extends Fragment {
        private final ToolContentDataAssessmentQuestion question;

        public OrderingInnerFragment(ToolContentDataAssessmentQuestion question) {
            this.question = question;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            Context c = getActivity().getApplicationContext();

            LinearLayout layout = new LinearLayout(c);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT, 0);

            layout.setLayoutParams(params);
            layout.setOrientation(LinearLayout.VERTICAL);

            // title
            Util.addWebView(getActivity(), layout, question.getTitle());

            // body
            Util.addWebView(getActivity(), layout, question.getBody());

            //

            // options
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1);
            for (ToolContentDataAssessmentQuestionOption option : question.getOptions()) {
                adapter.add(option.getStringValue());
            }
            DraggableListView lv = new DraggableListView(getActivity(), null);
            lv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            lv.setAdapter(adapter);
            lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

            layout.addView(lv);

            Util.setListViewHeightBasedOnChildren(lv);

            ScrollView scrollView = new ScrollView(getActivity());
            scrollView.addView(layout);
            return scrollView;
        }
    }

}
